#include <Arduino.h>
#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>

#include <ESP32_Heweather.h>
 
// JOSN是一种轻量级的数据交换格式，遵循一下规则：
// 1.并列的数据之间用逗号(,)分隔
// 2.映射用冒号(:)表示
// 3.并列数据的集合(数组)用方括号([])表示
// 4.映射的集合(对象)用大括号({})表示

// const char* ssid     = "HX_N";     // WiFi名称
// const char* password = "huaxundianzinj"; // WiFi密码


const char* ssid     = "ChinaNet-F765BC";     // WiFi名称
const char* password = "kcau4622"; // WiFi密码

 



String UserKey = "3108c4b4738a4ad08376f4053ef73dba";   // 私钥 https://dev.heweather.com/docs/start/get-api-key
String Location = "101190101"; // 城市代码 https://github.com/heweather/LocationList,表中的 Location_ID
String Unit = "m";             // 公制-m/英制-i
String Lang = "zh";            // 语言 英文-en/中文-zh
float ROUND = 0.5;             // 更新间隔<小时>空气质量API 1小时更新一次


 

WeatherNow weatherNow;
AirQuality airQuality;
WeatherForecast weatherForecast;


void ConnectWiFi(void);
 

void setup()
{

	Serial.begin(115200); // open the serial port at 115200 bps;
	delay(200);
	//启动wifi
	ConnectWiFi();
	Serial.println("WiFi connected");
	Serial.println("IP address: ");
	Serial.println(WiFi.localIP());

  weatherNow.config(UserKey, Location, Unit, Lang); // 配置请求信息
	airQuality.config(UserKey, Location, Unit, Lang); // 配置请求信息
  weatherForecast.config(UserKey, Location, Unit, Lang); // 配置请求信息
}


void loop()
{
	//获取天气
  if(weatherNow.get()){ // 获取天气更新
    Serial.println(F("======Weahter Now Info======"));
    Serial.print("Server Response: ");
    Serial.println(weatherNow.getServerCode());  // 获取API状态码
    Serial.print(F("Last Update: "));
    Serial.println(weatherNow.getLastUpdate());  // 获取服务器更新天气信息时间
    Serial.print(F("Temperature: "));
    Serial.println(weatherNow.getTemp());        // 获取实况温度
    Serial.print(F("FeelsLike: "));
    Serial.println(weatherNow.getFeelLike());    // 获取实况体感温度
    Serial.print(F("Icon: "));
    Serial.println(weatherNow.getIcon());        // 获取当前天气图标代码
    Serial.print(F("Weather Now: "));
    Serial.println(weatherNow.getWeatherText()); // 获取实况天气状况的文字描述
    Serial.print(F("windDir: "));
    Serial.println(weatherNow.getWindDir());     // 获取实况风向
    Serial.print(F("WindScale: "));
    Serial.println(weatherNow.getWindScale());   // 获取实况风力等级
    Serial.print(F("Humidity: "));
    Serial.println(weatherNow.getHumidity());    // 获取实况相对湿度百分比数值
    Serial.print(F("Precip: "));
    Serial.println(weatherNow.getPrecip());      // 获取实况降水量,毫米
    Serial.println(F("========================"));
  } else {    // 更新失败
    Serial.println("Update Failed...");
    Serial.print("Server Response: ");
    Serial.println(weatherNow.getServerCode()); // 参考 https://dev.heweather.com/docs/start/status-code
  }


 if(airQuality.get()){ // 获取空气质量
    Serial.println(F("======AirQuality Info======"));
    Serial.print("Server Response: ");
    Serial.println(airQuality.getServerCode()); // 获取API状态码
    Serial.print(F("Last Update: "));
    Serial.println(airQuality.getLastUpdate()); // 获取服务器更新天气信息时间
    Serial.print(F("AirQuality Aqi: "));
    Serial.println(airQuality.getAqi());        // 实时空气质量指数
    Serial.print(F("Category: "));
    Serial.println(airQuality.getCategory());   // 实时空气质量指数级别
    Serial.print(F("Primary: "));
    Serial.println(airQuality.getPrimary());    // 实时空气质量的主要污染物，优时返回值为 NA
    Serial.println(F("========================"));
  } else {  // 更新失败
    Serial.println("Update Failed...");
    Serial.print("Server Response: ");
    Serial.println(airQuality.getServerCode()); // 参考 https://dev.heweather.com/docs/start/status-code
  }


if(weatherForecast.get()){ // 获取天气预报
    Serial.println(F("======WeatherForecast Info======"));
    Serial.print("Server Response: ");
    Serial.println(weatherForecast.getServerCode()); // 获取API状态码
    Serial.print(F("Last Update: "));
    Serial.println(weatherForecast.getLastUpdate()); // 获取服务器更新天气信息时间
    Serial.print(F("SunRise: "));
    Serial.println(weatherForecast.getSunRise(0));    // 日出时间
    Serial.println(weatherForecast.getSunRise(1));    // 日出时间
    Serial.println(weatherForecast.getSunRise(2));    // 日出时间
    Serial.print(F("TempMax: "));
    Serial.println(weatherForecast.getTempMax(0));   // 最高气温
    Serial.println(weatherForecast.getTempMax(1));   // 最高气温
    Serial.println(weatherForecast.getTempMax(2));   // 最高气温
    Serial.print(F("TempMin: "));
    Serial.println(weatherForecast.getTempMin(0));    // 最低气温
    Serial.println(weatherForecast.getTempMin(1));    // 最低气温
    Serial.println(weatherForecast.getTempMin(2));    // 最低气温

    Serial.print(F("Humidity: "));
    Serial.println(weatherForecast.getHumidity(0));    // 湿度
    Serial.println(weatherForecast.getHumidity(1));    // 湿度
    Serial.println(weatherForecast.getHumidity(2));    // 湿度

    Serial.print(F("UvIndex: "));
    Serial.println(weatherForecast.getUvIndex(0));    // 紫外线强度
    Serial.println(weatherForecast.getUvIndex(1));    // 紫外线强度
    Serial.println(weatherForecast.getUvIndex(2));    // 紫外线强度


    Serial.println(F("========================"));
  } else {  // 更新失败
    Serial.println("Update Failed...");
    Serial.print("Server Response: ");
    Serial.println(airQuality.getServerCode()); // 参考 https://dev.heweather.com/docs/start/status-code
  }



  delay(ROUND * 10000);

 
 
}

// 连接WiFi
void ConnectWiFi(void)
{
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  Serial.print("Connecting to ");
  Serial.print(ssid);

  int i = 0;
  while (WiFi.status() != WL_CONNECTED && (i < 10)) {
    delay(1000);
    Serial.print('.');
    i++;
  }
  if (i == 10) { // 10s失败
    Serial.println("WiFi Connection Failed");
  } else { // 成功
    Serial.println("");
    Serial.println("WiFi Connection Successful!");
    Serial.print("IP address:    ");
    Serial.println(WiFi.localIP());
  }
}